// pages/user/order/invoice/invoice.js
import {
  getOrderList
} from '../../../../api/order.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    rowCount: 0,
    page:1,
    id:'',
    selectList:[],
    price:0,
    isAll:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:Number(options.id)
    })
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  selAll(){
    if(!this.data.isAll){
      var list = [];
      var price = 0;
      for(let i = 0;i < this.data.list.length;i++){
        price = price + this.data.list[i].fullPrice;
        list.push(this.data.list[i].id);
        this.data.list[i].isSel = true
      }
      this.setData({
        price:price,
        selectList:list,
        isAll:true,
        list:this.data.list
      })
    }else{
      for(let i = 0;i < this.data.list.length;i++){
        this.data.list[i].isSel = false
      }
      this.setData({
        selectList:[],
        price:0,
        isAll:false,
        list:this.data.list
      })
    }
   
  },
  sel(e){
    const {id,index,price}=e.currentTarget.dataset
    console.log(this.data.selectList.indexOf(id))
    if(this.data.selectList.indexOf(id) === -1){
      this.data.selectList.push(id);
      this.data.price += price;
      this.data.list[index].isSel = true
    }else{
      this.data.selectList.splice(this.data.selectList.indexOf(id),1)
      this.data.price -= price;
      this.data.list[index].isSel = false
    }
    if(this.data.isAll){
      this.data.isAll = false
    }else if(this.data.list.length === this.data.selectList.length){
      this.data.isAll = true
    }
    this.setData({
      selectList:this.data.selectList,
      price:this.data.price,
      list:this.data.list,
      isAll:this.data.isAll
    })
  },
  getData() {
    getOrderList({page:this.data.page,size:10,type:1}).then(res => {
      for(let i = 0;i < res.list.length;i++){
        res.list[i].isSel = false
      }

      this.setData({
        list: this.data.list.concat(res.list),
        rowCount: res.rowCount
      })
    })
  },

  handleScrollBottom(e) {
    if (this.data.rowCount > this.data.list.length) {
      this.setData({
        page:this.data.page+=1,
        isAll:false
      })

      this.getData();
    }
  },
  jumpToOpen(){
    wx.navigateTo({
      url: '/pages/user/order/open/open?id='+this.data.id+'&price='+this.data.price+'&orderIds='+this.data.selectList.join(','),
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})